package com.pearl.jacksoncore.demo.core;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.type.ResolvedType;
import com.fasterxml.jackson.core.type.TypeReference;
import com.pearl.jacksoncore.demo.pojo.User;

import java.io.IOException;
import java.util.Iterator;

/**
 * @author TD
 * @version 1.0
 * @date 2024/3/19
 */
public class StudyObjectCodec extends ObjectCodec {
    @Override
    public Version version() {
        return null;
    }

    @Override
    public <T> T readValue(JsonParser jsonParser, Class<T> aClass) throws IOException {
        // 1. 创建用户对象
        User user = new User();
        // 2. 循环解析每一个元素
        while (!jsonParser.isClosed()) {
            // 解析到下一个元素
            JsonToken jsonToken = jsonParser.nextToken();
            System.out.println("当前解析到的令牌类型：" + jsonToken);
            // 2.1 如果是属性名称类型
            if (JsonToken.FIELD_NAME.equals(jsonToken)) {
                String currentName = jsonParser.currentName(); // 获取属性名
                System.out.println("属性名称：" + currentName);
                if ("id".equals(currentName) ) {
                    jsonParser.nextToken(); // 解析到下一个元素，即为属性对应的值
                    Long userId = jsonParser.getLongValue();
                    user.setId(userId);
                }

                if ("name".equals(currentName)) {
                    jsonParser.nextToken();
                    String userName = jsonParser.getValueAsString();
                    user.setName(userName);
                }
                if ("age".equals(currentName)) {
                    jsonParser.nextToken();
                    int age = jsonParser.getIntValue();
                    user.setAge(age);
                }
            }
        }
        // 3. 返回
        return (T) user;
    }

    @Override
    public <T extends TreeNode> T readTree(JsonParser jsonParser) throws IOException {
        return null;
    }

    @Override
    public <T> T readValue(JsonParser jsonParser, TypeReference<T> typeReference) throws IOException {
        return null;
    }

    @Override
    public <T> T readValue(JsonParser jsonParser, ResolvedType resolvedType) throws IOException {
        return null;
    }

    @Override
    public <T> Iterator<T> readValues(JsonParser jsonParser, Class<T> aClass) throws IOException {
        return null;
    }

    @Override
    public <T> Iterator<T> readValues(JsonParser jsonParser, TypeReference<T> typeReference) throws IOException {
        return null;
    }

    @Override
    public <T> Iterator<T> readValues(JsonParser jsonParser, ResolvedType resolvedType) throws IOException {
        return null;
    }

    @Override
    public void writeValue(JsonGenerator jsonGenerator, Object o) throws IOException {

    }


    @Override
    public void writeTree(JsonGenerator jsonGenerator, TreeNode treeNode) throws IOException {

    }

    @Override
    public TreeNode createObjectNode() {
        return null;
    }

    @Override
    public TreeNode createArrayNode() {
        return null;
    }

    @Override
    public JsonParser treeAsTokens(TreeNode treeNode) {
        return null;
    }

    @Override
    public <T> T treeToValue(TreeNode treeNode, Class<T> aClass) throws JsonProcessingException {
        return null;
    }
}
